package com.ylsk.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.constant.GlobalConstants;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.system.mapper.SysMsgMapper;
import com.ylsk.system.domain.SysMsg;
import com.ylsk.system.service.ISysMsgService;

/**
 * 消息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@Service
public class SysMsgServiceImpl implements ISysMsgService
{
    @Autowired
    private SysMsgMapper sysMsgMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    /**
     * 查询消息
     *
     * @param messageId 消息主键
     * @return 消息
     */
    @Override
    public SysMsg selectSysMsgByMessageId(Long messageId)
    {
        SysMsg sysMsg = sysMsgMapper.selectSysMsgByMessageId(messageId);
        if(null != sysMsg && sysMsg.getIsRead() == 0 ){
            sysMsg.setIsRead(1);
            sysMsg.setToTime(new Date());
            sysMsgMapper.updateSysMsg(sysMsg);
        }
        return sysMsg;
    }

    /**
     * 查询消息列表
     *
     * @param sysMsg 消息
     * @return 消息
     */
    @Override
    public List<SysMsg> selectSysMsgList(SysMsg sysMsg)
    {
        sysMsg.setToUser(SecurityUtils.getUserId());
        return sysMsgMapper.selectSysMsgList(sysMsg);
    }

    /**
     * 新增消息
     *
     * @param sysMsg 消息
     * @return 结果
     */
    @Override
    public int insertSysMsg(SysMsg sysMsg)
    {
        long snowId = snowIdConfig.getSnowId();
        sysMsg.setMessageId(snowId);
        sysMsg.setCreateBy(String.valueOf(GlobalConstants.ADMIN_ID));
        sysMsg.setCreateTime(DateUtils.getNowDate());
        sysMsg.setUpdateBy(String.valueOf(GlobalConstants.ADMIN_ID));
        sysMsg.setUpdateTime(new Date());
        sysMsg.setDelFlag("0");
        sysMsg.setIsRead(0);
        return sysMsgMapper.insertSysMsg(sysMsg);
    }

    /**
     * 修改消息
     *
     * @param sysMsg 消息
     * @return 结果
     */
    @Override
    public int updateSysMsg(SysMsg sysMsg)
    {
        sysMsg.setUpdateTime(DateUtils.getNowDate());
        return sysMsgMapper.updateSysMsg(sysMsg);
    }

    /**
     * 批量删除消息
     *
     * @param messageIds 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgByMessageIds(Long[] messageIds)
    {
        return sysMsgMapper.deleteSysMsgByMessageIds(messageIds);
    }

    /**
     * 删除消息信息
     *
     * @param messageId 消息主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgByMessageId(Long messageId)
    {
        return sysMsgMapper.deleteSysMsgByMessageId(messageId);
    }

    @Override
    public int batchRead(Long userId) {
        if(null == userId){
            userId = SecurityUtils.getUserId();
        }
        //查询当前人未读的数据
        QueryWrapper<SysMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_user",userId);
        queryWrapper.eq("is_read",0);
        SysMsg sysMsg = new SysMsg();
        sysMsg.setUpdateTime(new Date());
        sysMsg.setToTime(new Date());
        sysMsg.setIsRead(1);
        sysMsg.setParams(null);
        int update = sysMsgMapper.update(sysMsg, queryWrapper);
        return update;
    }
}
